#!/usr/bin/env python

# CheckTrace for pcbnew using check traces
# This is the action plugin interface
# Author:       mihu525




import wx
import os
from pcbnew import ActionPlugin, GetBoard, Refresh


class HelloFrame(wx.Frame):
    """
    A Frame that says Hello World
    """

    def __init__(self, *args, **kw):
        # ensure the parent's __init__ is called
        super(HelloFrame, self).__init__(*args, **kw)
        
        self.netChoices = []
        self.sel_list = []
        self.board = GetBoard()
        self.count = self.board.GetNetCount()
        self.net_info = self.board.GetNetInfo()
        for net in range(0,self.count):
            item = self.net_info.GetNetItem(net)
            self.netChoices.append(item.GetNetname())

        # create a panel in the frame
        pnl = wx.Panel(self)

        sortingSizer = wx.StaticBoxSizer( wx.StaticBox( pnl, wx.ID_ANY, u"Select Net" ), wx.VERTICAL )
        bSizer6 = wx.BoxSizer( wx.VERTICAL )

        self.m_netlist = wx.ListBox( sortingSizer.GetStaticBox(), wx.ID_ANY, (0, 0),( 50, 100), self.netChoices, wx.LB_NEEDED_SB|wx.LB_EXTENDED )
        
        bSizer6.Add( self.m_netlist, 0, wx.ALL|wx.EXPAND, 5 )

        sortingSizer.Add( bSizer6, 0, wx.ALL|wx.EXPAND, 5 )
       
        pnl.SetSizer(sortingSizer)
        sortingSizer.Fit( pnl )
        
        self.m_netlist.Bind( wx.EVT_LISTBOX, self.OnShowNet )
        
        
        # create a menu bar
        self.makeMenuBar()

        # and a status bar
        self.CreateStatusBar()
        self.SetStatusText("Welcome to wxPython!")

    def OnShowNet(self, event):
        self.old_sel_list = self.sel_list
        self.sel_list = self.m_netlist.GetSelections()
        for sel in self.old_sel_list:
            net = self.board.TracksInNet(sel)
            for track in net:
                track.ClearBrightened()
                
        for sel in self.sel_list:
            net = self.board.TracksInNet(sel)
            for track in net:
                track.SetBrightened()
                
        Refresh()
        
        
    def makeMenuBar(self):
        """
        A menu bar is composed of menus, which are composed of menu items.
        This method builds a set of menus and binds handlers to be called
        when the menu item is selected.
        """

        # Make a file menu with Hello and Exit items
        fileMenu = wx.Menu()
        # The "\t..." syntax defines an accelerator key that also triggers
        # the same event
        helloItem = fileMenu.Append(-1, "&Hello...\tCtrl-H",
                "Help string shown in status bar for this menu item")
        fileMenu.AppendSeparator()
        # When using a stock ID we don't need to specify the menu item's
        # label
        exitItem = fileMenu.Append(wx.ID_EXIT)

        # Now a help menu for the about item
        helpMenu = wx.Menu()
        aboutItem = helpMenu.Append(wx.ID_ABOUT)

        # Make the menu bar and add the two menus to it. The '&' defines
        # that the next letter is the "mnemonic" for the menu item. On the
        # platforms that support it those letters are underlined and can be
        # triggered from the keyboard.
        menuBar = wx.MenuBar()
        menuBar.Append(fileMenu, "&File")
        menuBar.Append(helpMenu, "&Help")

        # Give the menu bar to the frame
        self.SetMenuBar(menuBar)

        # Finally, associate a handler function with the EVT_MENU event for
        # each of the menu items. That means that when that menu item is
        # activated then the associated handler function will be called.
        self.Bind(wx.EVT_MENU, self.OnHello, helloItem)
        self.Bind(wx.EVT_MENU, self.OnExit,  exitItem)
        self.Bind(wx.EVT_MENU, self.OnAbout, aboutItem)

    def __del__(self):
        self.old_sel_list = self.sel_list
        for sel in self.old_sel_list:
            net = self.board.TracksInNet(sel)
            for track in net:
                track.ClearBrightened()    
        Refresh()
    
    def OnExit(self, event):
        """Close the frame, terminating the application."""
        self.Close(True)


    def OnHello(self, event):
        """Say hello to the user."""
        wx.MessageBox("Hello again from wxPython")


    def OnAbout(self, event):
        """Display an About Dialog"""
        wx.MessageBox("This is a wxPython Hello World sample",
                      "About Hello World 2",
                      wx.OK|wx.ICON_INFORMATION)


class CheckTrackPlugin(ActionPlugin):
    """Class that gathers the actionplugin stuff"""
    def defaults(self):
        self.name = "check Trace"
        self.category = "Modify PCB"
        self.description = "Check Trace on a PCB"
        

    def Run(self):
        frm1 = HelloFrame(None, title='Check Trace')
        frm1.Show()


CheckTrackPlugin().register()